package com.atguigu.test.dacahngmainshitidierji.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/*
 *
 * @Description:
 *
 * @auther: madonghui
 * @date: 22:01 2020/11/13
 * @param:
 * @return:  runnable和callable的区别
 *            《1.runnable没有返回值，callable的返回值是泛型类型
 *
 */
class MyThread implements Runnable{
    @Override
    public void run() {

    }
}

/*
 *
 * @Description:
 *
 * @auther: madonghui
 * @date: 22:04 2020/11/13
 * @param:
 * @return: 场景---银行对账用的是批处理，开启多线程进行批处理，当有错误的时候需要返回错误信息，此时需要用带返回值的接口对象
 *
 */
class MyThread1 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("===================开炮啊=====================");
        return 1;
    }
}
/**
 * @Author: 19shusheng.com
 * @Description:
 * @Date: Created in 11:10 2020/11/14
 * @Modified By:
 */
public class CallableDeam {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ////此处有一个思想是适配思想
        FutureTask<Integer> integerFutureTask = new FutureTask<>(new MyThread1());
        //thread类的构造器只能传入实现了runnable的接口，FutureTask既实现了runnable也实现了callable此时就能用了(适配器的使用)
        //多线程实现的三种方法：继承thread，实现runnable，实现callable
        Thread thread = new Thread(integerFutureTask,"AA");
        thread.start();
        int result01 = 10;
        while (!integerFutureTask.isDone()){
            Integer integer = integerFutureTask.get();
            //get的意思：要求获得callable线程的计算结果，如果没有计算完成就去强求会导致线程阻塞直到计算完成为止
            System.out.println(".................result:"+(result01+integer));
        }
    }
}
